package com.springboot.film.controller;

import com.springboot.film.service.looktimeService;
import com.springboot.film.service.userinfoService;
import com.springboot.film.service.filmsService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class vedioCtrl {

    @Autowired
    private userinfoService userinfoService;
    @Autowired
    private looktimeService looktimeService;
    @Autowired
    private filmsService filmsService;

    //分页显示全部
    @RequestMapping("/vedio")
    public String films(Model model, String src, String fid, String vip_look, HttpSession session) {
        String username=(String)session.getAttribute("username");
        int fid_temp = Integer.parseInt(fid);
        filmsService.updatehot(fid_temp);
        int vip=0;
        if ("会员专享".equals(vip_look)){
            if(username == null){
                model.addAttribute("look", "nologin");
                model.addAttribute("src", "no");
            }else {
                vip=userinfoService.getvip(username);
                if(vip==1){
                    model.addAttribute("look", "yes");
                    model.addAttribute("src", src);
                }else {
                    model.addAttribute("look", "novip");
                    model.addAttribute("src", "no");
                }
            }
        }else{
            model.addAttribute("look", "yes");
            model.addAttribute("src", src);
        }
        model.addAttribute("fid", fid);
        return "vedio";
    }

    //添加
    //接收并返回json格式数据
    @RequestMapping("/addlooktime")
    @ResponseBody
    public String addlooktime(String fid,String time_temp,HttpSession session) {
        JSONObject jsonObject = new JSONObject();
        int fid_temp = Integer.parseInt(fid);
        int time_temp_temp = Integer.parseInt(time_temp);
        if (session.getAttribute("userid") != null){
            int userid=(int)session.getAttribute("userid");
            int countid=looktimeService.getcountid(userid,fid_temp);
            if(countid>0){
                int maxid=looktimeService.getmaxid(userid,fid_temp);
                looktimeService.updatelooktime(time_temp_temp,maxid);
            }else{
                looktimeService.add(userid,fid_temp,time_temp_temp);
            }
            jsonObject.put("code", "1");
        }else{
            jsonObject.put("code", "2");
        }
        return jsonObject.toString();
    }

    @RequestMapping("/update_fid")
    public String update_fid(String fid_new,String time_temp,HttpSession session) {
        int userid=(int)session.getAttribute("userid");
        int fid_new_temp = Integer.parseInt(fid_new);
        int time_temp_new = Integer.parseInt(time_temp);
        int countid=looktimeService.getcountid(userid,fid_new_temp);
        if(countid>0){
            time_temp_new=looktimeService.getmaxtime(userid,fid_new_temp);
        }
        int fid1 = userinfoService.getfid(userid,1);
        int fid2 = userinfoService.getfid(userid,2);
        int fid3 = userinfoService.getfid(userid,3);
        int fid4 = userinfoService.getfid(userid,4);
        int fid1_time=0,fid2_time=0,fid3_time=0,fid4_time=0;
        if(fid1 == 0){
            userinfoService.updatefid(fid_new_temp,fid1,fid2,fid3,userid);
        }else if(fid2 == 0){
            fid1_time = looktimeService.getmaxtime(userid,fid1);
            if(fid1 != fid_new_temp){
                if(time_temp_new>fid1_time){
                    userinfoService.updatefid(fid_new_temp,fid1,fid2,fid3,userid);
                }else{
                    userinfoService.updatefid(fid1,fid_new_temp,fid2,fid3,userid);
                }
            }
        }else if(fid3 == 0){
            fid1_time = looktimeService.getmaxtime(userid,fid1);
            fid2_time = looktimeService.getmaxtime(userid,fid2);
            if(fid1 != fid_new_temp && fid2 != fid_new_temp){
                if(time_temp_new>fid1_time){
                    userinfoService.updatefid(fid_new_temp,fid1,fid2,fid3,userid);
                }else if (time_temp_new>fid2_time){
                    userinfoService.updatefid(fid1,fid_new_temp,fid2,fid3,userid);
                }else{
                    userinfoService.updatefid(fid1,fid2,fid_new_temp,fid3,userid);
                }
            }else{
                if(fid2_time>fid1_time){
                    userinfoService.updatefid(fid2,fid1,fid3,fid4,userid);
                }
            }
        }else if(fid4 == 0){
            fid1_time = looktimeService.getmaxtime(userid,fid1);
            fid2_time = looktimeService.getmaxtime(userid,fid2);
            fid3_time = looktimeService.getmaxtime(userid,fid3);
            if(fid1 != fid_new_temp && fid2 != fid_new_temp && fid3 != fid_new_temp){
                if(time_temp_new>fid1_time){
                    userinfoService.updatefid(fid_new_temp,fid1,fid2,fid3,userid);
                }else if (time_temp_new>fid2_time){
                    userinfoService.updatefid(fid1,fid_new_temp,fid2,fid3,userid);
                }else if (time_temp_new>fid3_time){
                    userinfoService.updatefid(fid1,fid2,fid_new_temp,fid3,userid);
                }else{
                    userinfoService.updatefid(fid1,fid2,fid3,fid_new_temp,userid);
                }
            }else{
                if(fid2_time>fid1_time){
                    userinfoService.updatefid(fid2,fid1,fid3,fid4,userid);
                }else if (fid3_time>fid2_time){
                    if (fid3_time>fid1_time){
                        userinfoService.updatefid(fid3,fid1,fid2,fid4,userid);
                    }else{
                        userinfoService.updatefid(fid1,fid3,fid2,fid4,userid);
                    }
                }
            }

        }else if(fid1 != fid_new_temp && fid2 != fid_new_temp && fid3 != fid_new_temp && fid4 != fid_new_temp){
            fid1_time = looktimeService.getmaxtime(userid,fid1);
            fid2_time = looktimeService.getmaxtime(userid,fid2);
            fid3_time = looktimeService.getmaxtime(userid,fid3);
            fid4_time = looktimeService.getmaxtime(userid,fid4);
            if(time_temp_new>fid1_time){
                userinfoService.updatefid(fid_new_temp,fid1,fid2,fid3,userid);
            }else if (time_temp_new>fid2_time){
                userinfoService.updatefid(fid1,fid_new_temp,fid2,fid3,userid);
            }else if (time_temp_new>fid3_time){
                userinfoService.updatefid(fid1,fid2,fid_new_temp,fid3,userid);
            }else if (time_temp_new>fid4_time){
                userinfoService.updatefid(fid1,fid2,fid3,fid_new_temp,userid);
            }
        }else{
            fid1_time = looktimeService.getmaxtime(userid,fid1);
            fid2_time = looktimeService.getmaxtime(userid,fid2);
            fid3_time = looktimeService.getmaxtime(userid,fid3);
            fid4_time = looktimeService.getmaxtime(userid,fid4);
            if(fid2_time>fid1_time){
                userinfoService.updatefid(fid2,fid1,fid3,fid4,userid);
            }else if (fid3_time>fid2_time){
                if (fid3_time>fid1_time){
                    userinfoService.updatefid(fid3,fid1,fid2,fid4,userid);
                }else{
                    userinfoService.updatefid(fid1,fid3,fid2,fid4,userid);
                }
            }else if (fid4_time>fid3_time){
                if (fid4_time>fid2_time){
                    if(fid4_time>fid1_time){
                        userinfoService.updatefid(fid4,fid1,fid2,fid3,userid);
                    }else {
                        userinfoService.updatefid(fid1,fid4,fid2,fid3,userid);
                    }
                }else{
                    userinfoService.updatefid(fid1,fid2,fid4,fid3,userid);
                }
            }
        }
        return "redirect:/home";
    }

}
